let utils = require('utils');

cc.Class({
    extends: cc.Component,

    properties: {
        _block: {
            type:cc.Node,
            default:null
        },
        blockPrefab: {
            type:cc.Prefab,
            default:null
        },
        work: {
            type:cc.Node,
            default:null
        },
        _isDown:false
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        // this.node.on(cc.Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
    },

    start () {
    },

    // update (dt) {},

    onMouseDown(event) {
        this._isDown = true;
        this.node.once(cc.Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
    },
    onMouseUp(event) {
        this._isDown = false;
        this._block = null;
    },
    onMouseMove(event) {
        if (this._isDown) {
            if (this._block===null) {
                this._block = cc.instantiate(this.blockPrefab);
                this._block.parent = this.node;
                this._block.x = 0;
                this._block.y = 0;
                this.work.getComponent("BlockMoveScript").addMoveBlock(this._block);
                this.onMouseUp(null);
            }
            // if (this._block) {
            //     this._block.position = this.node.convertToNodeSpaceAR(event.getLocation());
            //     cc.log(this._block.position);
            // }
        }
    },
});
